package com.example.tnot.Teacher.MinePart.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tnot.R;
import com.example.tnot.Teacher.MinePart.itembean.rv_SetListItem;

import java.util.List;

public class myadapter_tool extends RecyclerView.Adapter<myadapter_tool.ViewHolder> {
    //recyclerView的数据源
    private List<rv_SetListItem> list;
    //构造方法，调用适配器的时候传入数据源
    public myadapter_tool(List<rv_SetListItem> list) {
        this.list = list;
    }
    private int currentPosition;
    @NonNull
    @Override
    public myadapter_tool.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_rv_me, parent, false);
        myadapter_tool.ViewHolder holder = new myadapter_tool.ViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull myadapter_tool.ViewHolder holder, int position) {
        holder.tv_message.setText(list.get(position).getText());
        holder.iv_icon.setImageResource(list.get(position).getImageResourceId());
        //单击跳转
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    NavController navController = Navigation.findNavController(v);
                    // 根据 position 决定导航到哪个 fragment
                    currentPosition = holder.getBindingAdapterPosition();
                    SwitchToSonFragment(currentPosition, navController);
            }
        });

//        //长按跳转
//        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                int currentPosition = holder.getBindingAdapterPosition();
//                Toast.makeText(v.getContext(), "长按了" + list.get(currentPosition).getText(), Toast.LENGTH_SHORT).show();
//                return true;
//            }
//        });
    }

    private void SwitchToSonFragment(int position, NavController navController) {
        switch (position) {
            case 0:
                navController.navigate(R.id.action_mineFragment_to_aboutstuFragment);
                break;
            case 1:
                navController.navigate(R.id.action_mineFragment_to_scoreFragment2);
                break;
            case 2:
                navController.navigate(R.id.action_mineFragment_to_NoteBookFragment);
                break;
//            case 3:
//                navController.navigate(R.id.action_mineFragment_to_hkconditionFragment);
//                break;
            case 3:
                navController.navigate(R.id.action_mineFragment_to_fileFragment);
                break;
            case 4:
                navController.navigate(R.id.action_mineFragment_to_collectFragment);
                break;
            case 5:
                navController.navigate(R.id.action_mineFragment_to_PrivacyFragment);
                break;
//            case 6:
//                navController.navigate(R.id.action_mineFragment_to_notmanageFragment);
//                break;
            case 6:
                navController.navigate(R.id.action_mineFragment_to_setFragment);
                break;
            // 添加更多 case 处理其他导航
            default:
                break;
        }
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_message;
        ImageView iv_icon;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_message = itemView.findViewById(R.id.tv_message);
            iv_icon = itemView.findViewById(R.id.iv_icon);
        }
    }
}
